﻿@using SIG.Data.Entity.Identity
@using SIG.Resources.Admin
@model IEnumerable<Role>
@{
    var modelName = EntityNames.Role;
    ViewBag.PageTitle = modelName;
    ViewBag.Title = ViewBag.PageTitle + "_安全";
}
 <div class="mb-2 text-right">
    <a asp-controller="Role" asp-action="EditRole" class="btn btn-primary" data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#edit-container"
       data-ajax-begin="onBegin" data-ajax-complete="onComplete" data-ajax-failure="onFailed" data-ajax-success="onSuccess"><i class="fa fa-plus fa-fw" data-icon="fa-plus"></i>添加角色</a>
</div>

    <table class="table table-striped table-bordered">
            <thead>
                <tr>
                  
                    <th>
                        角色名称
                    </th>
                    <th>
                        描述
                    </th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="RoleList">
               @Html.Partial("_List",Model)
            </tbody>
        </table>   


<div id="edit-container">
    @*@Html.Action("EditRole")*@
</div>

<div id="menus-container">    
</div>

<form id="anti-form" method="post">
    @Html.AntiForgeryToken()
</form>

@section footer {

    <script src="~/js/lib/jquery/plugins/jquery.validate.min.js"></script>
    <script src="~/js/lib/jquery/plugins/jquery.validate.unobtrusive.min.js"></script>
    <script src="~/js/lib/jquery/plugins/jquery.unobtrusive-ajax.min.js"></script>
    <script>
        function onBegin() {
            var iconEl = $(this).find("i.fa");
            var icon = iconEl.attr("data-icon");
            iconEl.removeClass(icon).addClass("fa-spinner fa-spin");
        }
        function onSuccess() {


        }
        function onComplete() {
            var iconEl = $(this).find("i.fa");
            var icon = iconEl.attr("data-icon");
            iconEl.removeClass("fa-spinner fa-spin").addClass(icon);
            $('#ModalEdit').modal();
        }
        function onFailed() {


        }

        function onAjaxComplete() {
            $('#ModalRoleForm').modal();
        }

        function onBeginSave() {
            $("#btnSave i").removeClass("fa-save").addClass("fa-spinner fa-spin");
        }
        function onCompleteSave() {
            $("#btnSave i").removeClass("fa-spinner fa-spin").addClass("fa-save");
        }
        function onSuccessSave(data) {
            var title = '@ViewBag.PageTitle';
            Common.ShowBoxWithFuncBack(data, title, submitSuccess);

        }
        function submitSuccess(id, htmlData) {
            $('#ModalEdit').modal('hide');          
            
            //   $("#" + cid).replaceWith(htmlData);
            var url = "@Url.Action("index")";
            $.get(url, function (result) {
                // console.log(result);
                $("#RoleList").replaceWith(result);
                //$("#" + cid).replaceWith(result);
            });
          
        }



        function onAjaxCompleteRoleMenus() {
            var iconEl = $(this).find("i.fa");
            var icon = iconEl.attr("data-icon");
            iconEl.removeClass("fa-spinner fa-spin").addClass(icon);
            $('#ModalEdit').modal();
           
            $(document).on('change','input[name=menuId]',function () {
                if ($(this).is(":checked")) {
                    var checkboxs = $(this).closest("li").find("input[name=menuId]").prop("checked", "checked");
                } else {
                    $(this).closest("li").find("input[name=menuId]").removeAttr("checked");
                }

            });
        }

        function updateSuccessRoleMenus(data) {
            Common.ShowBox(data.Status, data.Message, "@modelName");
            $('#ModalEdit').modal('hide');

        }



        $(function () {


            var modelName = "@modelName";

            //确认框默认语言
            bootbox.setDefaults({
                locale: "zh_CN"
            });


            //delete
            $(document).on("click", '.delete-item', function (e) {
                e.preventDefault();

                var url = $(this).attr('href');
                var that = $(this);
                bootbox.setLocale("zh_CN");
                bootbox.confirm("你确定要删除此项吗？", function (result) {
                    if (result) {
                        /*DELETE*/
                        var title = "@ViewBag.Title";
                        Common.DeleteItem(url, title, that);
                    }
                });

            });




        });
    </script>

}